package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.utils.DateUtils;
import com.ruibang.glass.produce.domain.req.UniformityMixtureReq;
import com.ruibang.glass.produce.domain.resp.DefectAnalysisAnnotation;
import com.ruibang.glass.produce.domain.resp.Hot2DefectAnalysisChart;
import com.ruibang.glass.produce.domain.resp.Hot2DefectAnalysisResp;
import com.ruibang.glass.produce.mapper.Hot2DefectAnalysisMapper;
import com.ruibang.glass.produce.service.Hot2DefectAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-05-29  09:54
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class Hot2DefectAnalysisServiceImpl implements Hot2DefectAnalysisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private Hot2DefectAnalysisMapper hot2DefectAnalysisMapper;

    @Autowired
    private Sequence sequence;


    @Override
    public void saveAnnotation(DefectAnalysisAnnotation defectAnalysisAnnotations) {
        if (StringUtils.isBlank(defectAnalysisAnnotations.getId())) {
            defectAnalysisAnnotations.setAnnotationTime(LocalDateTime.now().format(Constants.DATE_TIME_FORMATTER));
            defectAnalysisAnnotations.setId(String.valueOf(sequence.nextId()));
            redisTemplate.opsForList().rightPush(RedisKey.DEFECT_ANALYSIS_ANNOTATION, defectAnalysisAnnotations);
        } else {
            List<DefectAnalysisAnnotation> analysisAnnotationList = this.getAnnotation();
            for (DefectAnalysisAnnotation defectAnalysisAnnotation : analysisAnnotationList) {
                if (defectAnalysisAnnotations.getId().equals(defectAnalysisAnnotation.getId())) {
                    defectAnalysisAnnotation.setContent(defectAnalysisAnnotations.getContent());
                    defectAnalysisAnnotation.setX1(defectAnalysisAnnotations.getX1());
                    defectAnalysisAnnotation.setX2(defectAnalysisAnnotations.getX2());
                    defectAnalysisAnnotation.setAnnotationUserName(defectAnalysisAnnotations.getAnnotationUserName());
                }
            }
            redisTemplate.delete(RedisKey.DEFECT_ANALYSIS_ANNOTATION);
            redisTemplate.opsForList().rightPushAll(RedisKey.DEFECT_ANALYSIS_ANNOTATION, analysisAnnotationList.toArray());
        }
    }

    public List<DefectAnalysisAnnotation> getAnnotation() {
        List<Object> freDefectConfigObjList = redisTemplate.opsForList().range(RedisKey.DEFECT_ANALYSIS_ANNOTATION, 0, -1);
        if (CollectionUtils.isNotEmpty(freDefectConfigObjList)) {
            List<DefectAnalysisAnnotation> data = new ArrayList<>();
            for (Object obj : freDefectConfigObjList) {
                data.add((DefectAnalysisAnnotation) obj);
            }
            return data;
        }
        return Collections.emptyList();
    }

    @Override
    public Hot2DefectAnalysisChart defectAnalysisChart(UniformityMixtureReq uniformityMixtureReq) {
        Hot2DefectAnalysisChart hot2DefectAnalysisChart = new Hot2DefectAnalysisChart();
        if (uniformityMixtureReq.getStartTime() != null && uniformityMixtureReq.getEndTime() != null) {
            List<Hot2DefectAnalysisResp> hot2DefectAnalysisResps = hot2DefectAnalysisMapper.defectAnalysisChart(uniformityMixtureReq);
            if (CollectionUtils.isNotEmpty(hot2DefectAnalysisResps)) {
                //ptNum
                List<Integer> pt = hot2DefectAnalysisResps.stream().map(Hot2DefectAnalysisResp::getPtNum).collect(Collectors.toList());
                hot2DefectAnalysisChart.setPt(pt);
                //stoneNum
                List<Integer> stone = hot2DefectAnalysisResps.stream().map(Hot2DefectAnalysisResp::getStoneNum).collect(Collectors.toList());
                hot2DefectAnalysisChart.setStone(stone);
                //bubbleNum
                List<Integer> bubble = hot2DefectAnalysisResps.stream().map(Hot2DefectAnalysisResp::getBubbleNum).collect(Collectors.toList());
                hot2DefectAnalysisChart.setBubble(bubble);
                //采集时间
                List<String> collectTime = hot2DefectAnalysisResps.stream().map(e -> e.getStartTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
                hot2DefectAnalysisChart.setCollectTime(collectTime);
            }
        }
        return hot2DefectAnalysisChart;
    }

    @Override
    public void deleteById(String id) {
        List<DefectAnalysisAnnotation> annotation = this.getAnnotation();
        if (CollectionUtils.isNotEmpty(annotation)) {
            List<DefectAnalysisAnnotation> data = annotation.stream().filter(e -> !e.getId().equals(id)).collect(Collectors.toList());
            redisTemplate.delete(RedisKey.DEFECT_ANALYSIS_ANNOTATION);
            if (CollectionUtils.isNotEmpty(data)) {
                redisTemplate.opsForList().rightPushAll(RedisKey.DEFECT_ANALYSIS_ANNOTATION, data.toArray());
            }
        }
    }

    @Override
    public List<DefectAnalysisAnnotation> queryAnnotation(UniformityMixtureReq uniformityMixtureReq) {
        List<DefectAnalysisAnnotation> defectAnalysisAnnotationList = this.getAnnotation();
        if (uniformityMixtureReq.getStartTime() != null && uniformityMixtureReq.getEndTime() != null && CollectionUtils.isNotEmpty(defectAnalysisAnnotationList)) {
            return defectAnalysisAnnotationList.stream().filter(e -> {
                LocalDateTime x1 = DateUtils.stringToLocalDateTime(e.getX1(), Constants.DATE_TIME_FORMATTER_STR);
                LocalDateTime x2 = DateUtils.stringToLocalDateTime(e.getX2(), Constants.DATE_TIME_FORMATTER_STR);
                if ((x1.compareTo(uniformityMixtureReq.getStartTime()) >= 0 && x1.compareTo(uniformityMixtureReq.getEndTime()) <= 0)
                        && (x2.compareTo(uniformityMixtureReq.getStartTime()) >= 0 && x2.compareTo(uniformityMixtureReq.getEndTime()) <= 0)) {
                    return true;
                }
                return false;
            }).collect(Collectors.toList());

        }
        return Collections.emptyList();
    }

}
